package cn.micro.system.manage.annotate.impl;

import cn.micro.system.manage.annotate.UniqueProduct;
import cn.micro.system.manage.annotate.UniqueProfession;
import cn.micro.system.manage.entity.AssetDimProductInfo;
import cn.micro.system.manage.entity.SysDimProfession;
import cn.micro.system.manage.service.AssetDimProductInfoService;
import cn.micro.system.manage.service.SysDimProfessionService;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class ProductValidation  <T extends Annotation> implements ConstraintValidator<T, AssetDimProductInfo> {

    protected Predicate<AssetDimProductInfo> predicate = c -> true;

    @Resource
    protected AssetDimProductInfoService assetDimProductInfoService;

    @Override
    public boolean isValid(AssetDimProductInfo assetDimProductInfo, ConstraintValidatorContext constraintValidatorContext) {
        return assetDimProductInfoService == null || predicate.test(assetDimProductInfo);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class UniqueProductValidate extends ProductValidation<UniqueProduct>{
        @Override
        public void initialize(UniqueProduct constraintAnnotation) {
            predicate = c -> assetDimProductInfoService.getByCodeOrName(c).isEmpty();
        }
    }
}
